/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fresher.profilerLogUtility;

import com.vng.jcore.profiler.ProfilerLog;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author quangtm5
 */
public class WriteProfilerLog 
{
    private static WriteProfilerLog writeProfilerLogInstance = null;
    private File fileToWrite = null;
    private FileWriter fileWriter = null;
    private BufferedWriter bufferedWriter = null;
    private String destinationPath = "/home/namlq2/Desktop/log.xlsx";
    
    
    private WriteProfilerLog() 
    {
       fileToWrite = new File(destinationPath);
       
       // if file doesn't exists, then create it
       if(!fileToWrite.exists())
       {
           try 
           {
               fileToWrite.createNewFile();
           } 
           catch (IOException ex) 
           {
               Logger.getLogger(WriteProfilerLog.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
        try {
            //set second argument = true to let FileWriter know that
            //just append to end of file rather than begging of file
            fileWriter = new FileWriter(fileToWrite,true);
            bufferedWriter = new BufferedWriter(fileWriter);
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(WriteProfilerLog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static synchronized WriteProfilerLog getWriteProfilerLog()
    {
        if( writeProfilerLogInstance == null)
        {
            writeProfilerLogInstance = new WriteProfilerLog();
        }
        
        return writeProfilerLogInstance;
    }
    
    public String parseProfilerLogToExcelTemplate(ProfilerLog aProfilerLog)
    {
        String parsedString = "";
        String profilerLogString = aProfilerLog.dumpLogConsole();
        
      
        String[] splitString = profilerLogString.split("\n");
        
        for(int i = 3; i < splitString.length ; i++)
        {
            parsedString += splitString[i].replace("\t", ",");
            parsedString += "\n";
        }
        
        return parsedString;
    }
    
    public synchronized  void writeLogToFile(ProfilerLog aProfilerLog)
    {
        
        try {
            fileWriter.append(parseProfilerLogToExcelTemplate(aProfilerLog));
        } catch (IOException ex) {
            Logger.getLogger(WriteProfilerLog.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    public synchronized void saveToFile()
    {
        try {
            bufferedWriter.close();
            fileWriter = new FileWriter(fileToWrite,true);
            bufferedWriter = new BufferedWriter(fileWriter);
        } catch (IOException ex) {
            Logger.getLogger(WriteProfilerLog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
